package ducct.footprint.ui;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Process;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import ducct.footprint.R;
import ducct.footprint.db.DatabaseBackgroundWorker;
import ducct.footprint.db.DatabaseHelper;
import ducct.footprint.entity.MyFootprint;
import ducct.footprint.location.MyLocationListener;
import ducct.footprint.ui.adapter.MyFootprintAdapter;
import ducct.footprint.utils.Global;

public class MyFootprintListActivity extends Activity {
	private List<MyFootprint> footprints;
	
	private static MyFootprintListActivity instance = null;
	
	public static MyFootprintListActivity getInstance() {
		return instance;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		instance = this;
		
		// init
		Global.db = DatabaseHelper.getInstance(this);
		MyLocationListener.getInstance();
		DatabaseBackgroundWorker.startWaitingForDbTask();
		
		// loading
		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        setContentView(R.layout.loading);
        setProgressBarVisibility(true);
        
        // load footprints and display
        new Thread() {
        	public void run() {
        		footprints = Global.db.getAllMyFootprints();
        		runOnUiThread(new Runnable() {
					public void run() {
						displayMyFootprints();
					}
				});
        	}
        }.start();
	}
	
	private void displayMyFootprints() {
		setContentView(R.layout.my_footprint_list);
		
		ListView list = (ListView) findViewById(R.id.list);
		MyFootprintAdapter adapter = new MyFootprintAdapter(this, footprints);
		list.setAdapter(adapter);
		
		Button btnCaptureImage = (Button) findViewById(R.id.btnCaptureImage);
		btnCaptureImage.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				startActivity(new Intent(MyFootprintListActivity.this, CaptureImageActivity.class));
			}
		});
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		
		Process.killProcess(Process.myPid());
	}
}
